﻿#include "Login.h"
#include <QPainter>
#include <QStandardItemModel>
#include <QtXml>
#include<qmessagebox.h>
#include"UserManagerForm.h"
Login::Login(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    connect(ui.pushButton_manager, &QPushButton::clicked, this, &Login::on_Manager_Btn_clicked);
    connect(ui.pushButton_login, &QPushButton::clicked, this, &Login::on_Login_Btn_clicked);
    connect(ui.comboBox_userName, &QComboBox::currentTextChanged, this, &Login::on_ComboboxIndexChange);
//    connect(ui.comboBox_userName, SIGNAL(QComboBox::currentTextChanged(QString)), this, SLOT(Login::on_ComboboxIndexChange(QString)));

    LoadUserInfo();
    UpdateUI();

}

Login::~Login()
{
    WriteUserInfo();
}

UserInfo Login::GetLoginUser()
{
    return _loginUser;
}
void Login::on_Manager_Btn_clicked()
{
    UserManagerForm managerForm;
    managerForm.setUserInfo(listUserInfo);
    int result =managerForm.exec();
    if (result==1)
    {
        listUserInfo = managerForm.getUserInfos();
        WriteUserInfo();
        UpdateUI();
    }

}

void Login::LoadUserInfo()
{

    QFile readFile(strUserInfoFile);
    if (!readFile.exists())
    {
        UserInfo userinfo;
        userinfo.setName("admin");
        userinfo.setPassWord("admin");
        listUserInfo.append(userinfo);
        WriteUserInfo();
        return;
    }
    else
    {
        //用户信息文件加载
        readFile.open(QIODevice::ReadOnly);
        QDataStream in(&readFile);
        UserInfo userinfo;
        while (true)
        {
            if (in.atEnd())
            {
                readFile.close();
                break;
            }
            in >> userinfo;
            listUserInfo.append(userinfo);

        }

        if(listUserInfo.count()==0)
        {
            UserInfo userinfo;
            userinfo.setName("admin");
            userinfo.setPassWord("admin");
            listUserInfo.append(userinfo);
            WriteUserInfo();
        }
        if (readFile.isOpen())
        {
            readFile.close();
        }
    }


}

void Login::WriteUserInfo()
{
    QFile writeFile(strUserInfoFile);
    writeFile.open(QIODevice::WriteOnly);
    QDataStream out(&writeFile);
    foreach (UserInfo var ,listUserInfo)
    {
        out << var;
    }

    writeFile.close();
}

void Login::UpdateUI()
{
    ui.comboBox_userName->clear();
    // 创建一个标准项模型
    QStandardItemModel *model = new QStandardItemModel(ui.comboBox_userName);

    // 添加一些项，并设置它们的文本居中对齐
    for (size_t i = 0; i < listUserInfo.length(); i++)
    {
        QStandardItem *item1 = new QStandardItem(listUserInfo[i].name());
        item1->setTextAlignment(Qt::AlignCenter);
        model->appendRow(item1);
    }
    ui.comboBox_userName->setModel(model);

}

void Login::on_Login_Btn_clicked()
{
    QString strUserName = ui.comboBox_userName->currentText();
    QString strPassWord = ui.lineEdit_passWord->text();
    bool bRight = false;
    foreach (UserInfo var ,listUserInfo)
    {
        if (var.name()==strUserName)
        {
            if (var.password()==strPassWord)
            {
                _loginUser = var;
                accept();
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setText(QString::fromLocal8Bit("密码错误."));
                msgBox.exec();
            }
        }
    }

}
QPixmap Login::getRoundRectPixmap(QPixmap srcPixMap, const QSize & size, int radius)
{
    //不处理空数据或者错误数据
    if (srcPixMap.isNull()) {
        return srcPixMap;
    }

    //获取图片尺寸
    int imageWidth = size.width();
    int imageHeight = size.height();

    //处理大尺寸的图片,保证图片显示区域完整
    QPixmap newPixMap = srcPixMap.scaled(imageWidth, (imageHeight == 0 ? imageWidth : imageHeight),Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    QPixmap destImage(imageWidth, imageHeight);
    destImage.fill(Qt::transparent);
    QPainter painter(&destImage);
    // 抗锯齿
    painter.setRenderHints(QPainter::Antialiasing, true);
    // 图片平滑处理
    painter.setRenderHints(QPainter::SmoothPixmapTransform, true);
    // 将图片裁剪为圆角
    QPainterPath path;
    QRect rect(0, 0, imageWidth, imageHeight);
    path.addRoundedRect(rect, radius, radius);
    painter.setClipPath(path);
    painter.drawPixmap(0, 0, imageWidth, imageHeight, newPixMap);
    return destImage;
}

void Login::on_ComboboxIndexChange(const QString& text)
{
    QString userName =text;
    qDebug()<<text<<endl;
    for (int i = 0; i < listUserInfo.count(); ++i)
    {
        if(listUserInfo[i].name()==userName)
        {
            if(listUserInfo[i].iconPath().isEmpty())
                continue;
            // 加载图片
            QPixmap pixmap(listUserInfo[i].iconPath()); // 替换为你的图片路径

            auto pixmapKeepAspectRatioByExpanding=getRoundRectPixmap(pixmap,ui.label_UserIcon->size(),ui.label_UserIcon->width()/2);
            ui.label_UserIcon->setPixmap(pixmapKeepAspectRatioByExpanding);
            ui.label_UserIcon->update();
            qDebug()<<"update icon"<<endl;

            //更新操作等级
            ui.lineEdit_type->setText(listUserInfo[i].OperatorLevel());
        }
    }

}

